using log4net;
using Mathmall.Administration.Service;
using Mathmall.Administration.Vo;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using JsonSerializer = System.Text.Json.JsonSerializer;

namespace Mathmall.Administration.Controllers;

[ApiController]
[Route("api/[controller]")]
public class MenuController : ControllerBase
{
    private readonly IMenuService _menuService;
    private static readonly ILog _logger = LogManager.GetLogger(typeof(MenuController));

    public MenuController(IMenuService menuService)
    {
        _menuService = menuService;
    }

    /// <summary>
    /// 添加菜单
    /// </summary>
    /// <param name="newMenu"></param>
    /// <returns></returns>
    [Authorize(Policy = "AddMenu")]
    [HttpPost]
    public async Task<int> AddMenu(MenuVo newMenu)
    {
        return await _menuService.AddMenu(newMenu);
    }

    /// <summary>
    /// 查询全部菜单，以树形结构返回
    /// </summary>
    /// <returns></returns>
    [Authorize(Policy = "QueryMenu")]
    [HttpGet]
    public async Task<List<MenuVo>> GetMenu()
    {
        List<MenuVo> menuVos = await _menuService.GetMenus();

        return menuVos;
    }

    /// <summary>
    /// 更新菜单
    /// </summary>
    /// <param name="newMenu"></param>
    /// <returns></returns>
    [Authorize(Policy = "EditMenu")]
    [HttpPut]
    public async Task<int> UpdateMenu(MenuVo newMenu)
    {
        return await _menuService.UpdateMenu(newMenu);
    }

    /// <summary>
    /// 删除菜单
    /// </summary>
    /// <param name="menuID"></param>
    /// <returns></returns>
    [Authorize(Policy = "DeleteMenu")]
    [HttpDelete("{menuID}")]
    public async Task<int> DeleteMenu(int menuID)
    {
        return await _menuService.DeleteMenu(menuID);
    }
}